package com.bank.controller;


import com.bank.auto.JSONAuthentication;
import com.bank.entity.*;
import com.bank.mapper.BaDepositMapper;
import com.bank.mapper.BaMeansMapper;
import com.bank.mapper.BaUserMapper;
import com.bank.response.Result;
import com.bank.service.BaDepositService;
import com.bank.utils.ExcelUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bank
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/ba-deposit")
public class BaDepositController extends JSONAuthentication {
    @Autowired
    BaDepositMapper depositMapper;
    @Autowired
    BaUserMapper baUserMapper;
    @Autowired
    BaMeansMapper baMeansMapper;
    @Autowired
    BaDepositService baDepositService;
    @ApiOperation(value = "用户存款")
    @PostMapping("/add")
    public void addDraw(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("price") float price,@RequestParam("password") String password) throws Exception {
       QueryWrapper wrappera=new QueryWrapper();
       wrappera.eq("user_id",userId);
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        if(encoder.matches(password,baMeansMapper.selectOne(wrappera).getDrawPassword())){
            BaMeans baMeans=  baMeansMapper.selectOne(wrappera);
            float balance= baMeans.getBalance();
            BaDeposit baDeposit=new BaDeposit();
            baDeposit.setUserId(userId);
            baDeposit.setPrice(price);
            Date date=new Date();
            Timestamp timestamp=new Timestamp(date.getTime());
            baDeposit.setCreatTime(timestamp);
            baMeans.setBalance(balance+price);
            if(baMeansMapper.updateById(baMeans)>0&&depositMapper.insert(baDeposit)>0){
                Result ok = Result.ok().message("存款成功");
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error().message("存款失败");
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error().message("密码错误");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找存款取款记录")
    @PostMapping("/findAllDraw")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<BaDeposit> shUserPage=new Page<>(current,size);
        Page<BaDeposit> shUserPages=depositMapper.selectPage(shUserPage,null);
        List<BaDeposit> records = shUserPages.getRecords();
        int total=baUserMapper.selectCount(null);
        for(BaDeposit a:records){
            BaUser baUser=baUserMapper.selectById(a.getUserId());
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("user_id",a.getUserId());
            if(baMeansMapper.selectOne(wrapper)!=null){
                baUser.setBaMeans(baMeansMapper.selectOne(wrapper));
                a.setBaUser(baUser);
            }

        }
        if(records.size()>0){
            Result ok = Result.ok().data("depos",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找存款记录通过用户id")
    @PostMapping("/findAllDrawByUserId")
    public void findAllDrawByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("user_id",userId);
        Page<BaDeposit> shUserPage=new Page<>(current,size);
        Page<BaDeposit> shUserPages=depositMapper.selectPage(shUserPage,wrapper1);
        List<BaDeposit> records = shUserPages.getRecords();
        int total=baUserMapper.selectCount(wrapper1);
        for(BaDeposit a:records){
            BaUser baUser=baUserMapper.selectById(a.getUserId());
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("user_id",a.getUserId());
            if(baMeansMapper.selectOne(wrapper)!=null){
                baUser.setBaMeans(baMeansMapper.selectOne(wrapper));
                a.setBaUser(baUser);
            }

        }
        if(records.size()>0){
            Result ok = Result.ok().data("Draw",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "按月份查找取钱的总金额")
    @PostMapping("/findAndMonth")
    public void findAndMonth(HttpServletRequest request, HttpServletResponse response)throws  Exception {
        List<TuXIng> andMonth = depositMapper.findAndMonth();
        Result ok = Result.ok().data("Depost",andMonth);
        this.WriteJSON(request, response, ok);
    }


}

